import { expect } from 'chai';
import getValueAt from '../src/getValueAt.js';

describe('getValueAt', function () {
    it('should return undefined if object not exist', function () {
        expect(getValueAt()).to.be.eql(undefined);
    });

    it('should return itself if key is empty', function () {
        const obj = { name: 'foo' };
        expect(getValueAt(obj)).to.be.eql(obj);
    });

    it('should return foo if key does exist', function () {
        const obj = { name: 'foo' };
        expect(getValueAt(obj, 'name')).to.be.eql('foo');
    });

    it('should return undefiend if key does not exist', function () {
        const obj = { name: 'foo' };
        expect(getValueAt(obj, 'foo')).to.be.eql(undefined);
    });

    it('should return foo', function () {
        const obj = {
            user: {
                name: 'foo',
            },
        };

        expect(getValueAt(obj, 'user.name')).to.be.eql('foo');
    });

    it('should return undefined', function () {
        const obj = {
            user: {
                name: 'foo',
            },
        };

        expect(getValueAt(obj, 'user.name.foo')).to.be.eql(undefined);
    });

    it('should return default', function () {
        const obj = {
            user: {
                name: 'foo',
            },
        };

        expect(getValueAt(obj, 'user.name.foo', 'default')).to.be.eql(
            'default'
        );
    });
});
